package com.sl.biz.core.openapi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.sl.biz.modular.product.dao.entity.ProductDO;
import com.sl.biz.modular.product.service.ProductBizService;
import org.springdoc.core.properties.SpringDocConfigProperties;
import org.springdoc.core.properties.SwaggerUiConfigParameters;
import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springdoc.core.providers.SpringWebProvider;
import org.springdoc.webmvc.ui.SwaggerWelcomeWebMvc;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller
public class CustomerSwaggerWelcomeWebMvc extends SwaggerWelcomeWebMvc {

    private final ProductBizService productBizService;

    public CustomerSwaggerWelcomeWebMvc(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties, SwaggerUiConfigParameters swaggerUiConfigParameters,
                                        SpringWebProvider springWebProvider, ProductBizService productBizService) {
        super(swaggerUiConfig, springDocConfigProperties, swaggerUiConfigParameters, springWebProvider);
        this.productBizService = productBizService;
    }

    @Override
    protected void calculateUiRootPath(StringBuilder... sbUrls) {
        IgnoreStrategy ignoreStrategy = IgnoreStrategy.builder().tenantLine(true).build();
        InterceptorIgnoreHelper.handle(ignoreStrategy);
        List<ProductDO> list = productBizService.list();
        InterceptorIgnoreHelper.clearIgnoreStrategy();
        if (CollUtil.isEmpty(list)) {
            super.calculateUiRootPath(sbUrls);
            return;
        }

        for (ProductDO productDO : list) {
            String productCode = productDO.getProductCode();
            if(StrUtil.isBlank(productCode)){
                continue;
            }
            swaggerUiConfigParameters.addGroup(productDO.getProductCode(), productDO.getProductCode());
        }
        super.calculateUiRootPath(sbUrls);
    }

}
